/*
 * CarcassonneView.java
 */

package carcassonne.gui;

import carcassonne.gameplay.Game;
import carcassonne.gameplay.listObjects.NextAvailablePositions;
import java.awt.Image;
import java.awt.Toolkit;
import org.jdesktop.application.Action;
import org.jdesktop.application.ResourceMap;
import org.jdesktop.application.SingleFrameApplication;
import org.jdesktop.application.FrameView;
import org.jdesktop.application.TaskMonitor;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.net.URL;
import javax.swing.Timer;
import javax.swing.Icon;
import javax.swing.JDialog;
import javax.swing.JFrame;

/**
 * The application's main frame.
 */
public class CarcassonneView extends FrameView {
	//image loading
	private static final String BASE_IMAGE_URL_LOADER = "BaseImageURLLoader";
	private static final String MAIN_BACKGROUND_IMAGE_NAME = "mainBackground.png";
	private static final String MAIN_MENU_BUTTON_IMAGE_NAME = "mainMenuButton.png";
	
	//button text
	private static final String START_GAME_BUTTON_TEXT = "Start Game";
	private static final String SETTINGS_BUTTON_TEXT = "Settings";

	private Image backgroundImage;
	
    public CarcassonneView(SingleFrameApplication app) {
        super(app);

		URL url = this.getClass().getClassLoader().getResource(this.getResourceMap().getString(CarcassonneView.BASE_IMAGE_URL_LOADER, CarcassonneView.MAIN_BACKGROUND_IMAGE_NAME));
		this.setBackgroundImage(Toolkit.getDefaultToolkit().getImage(url));
		
        initComponents();

        // status bar initialization - message timeout, idle icon and busy animation, etc
        ResourceMap resourceMap = getResourceMap();
        int messageTimeout = resourceMap.getInteger("StatusBar.messageTimeout");
        messageTimer = new Timer(messageTimeout, new ActionListener() {
            public void actionPerformed(ActionEvent e) {
                statusMessageLabel.setText("");
            }
        });
        messageTimer.setRepeats(false);
        int busyAnimationRate = resourceMap.getInteger("StatusBar.busyAnimationRate");
        for (int i = 0; i < busyIcons.length; i++) {
            busyIcons[i] = resourceMap.getIcon("StatusBar.busyIcons[" + i + "]");
        }
        busyIconTimer = new Timer(busyAnimationRate, new ActionListener() {
            public void actionPerformed(ActionEvent e) {
                busyIconIndex = (busyIconIndex + 1) % busyIcons.length;
                statusAnimationLabel.setIcon(busyIcons[busyIconIndex]);
            }
        });
        idleIcon = resourceMap.getIcon("StatusBar.idleIcon");
        statusAnimationLabel.setIcon(idleIcon);
        progressBar.setVisible(false);

        // connecting action tasks to status bar via TaskMonitor
        TaskMonitor taskMonitor = new TaskMonitor(getApplication().getContext());
        taskMonitor.addPropertyChangeListener(new java.beans.PropertyChangeListener() {
            public void propertyChange(java.beans.PropertyChangeEvent evt) {
                String propertyName = evt.getPropertyName();
                if ("started".equals(propertyName)) {
                    if (!busyIconTimer.isRunning()) {
                        statusAnimationLabel.setIcon(busyIcons[0]);
                        busyIconIndex = 0;
                        busyIconTimer.start();
                    }
                    progressBar.setVisible(true);
                    progressBar.setIndeterminate(true);
                } else if ("done".equals(propertyName)) {
                    busyIconTimer.stop();
                    statusAnimationLabel.setIcon(idleIcon);
                    progressBar.setVisible(false);
                    progressBar.setValue(0);
                } else if ("message".equals(propertyName)) {
                    String text = (String)(evt.getNewValue());
                    statusMessageLabel.setText((text == null) ? "" : text);
                    messageTimer.restart();
                } else if ("progress".equals(propertyName)) {
                    int value = (Integer)(evt.getNewValue());
                    progressBar.setVisible(true);
                    progressBar.setIndeterminate(false);
                    progressBar.setValue(value);
                }
            }
        });
    }

    @Action
    public void showAboutBox() {
        if (aboutBox == null) {
            JFrame mainFrame = CarcassonneApp.getApplication().getMainFrame();
            aboutBox = new CarcassonneAboutBox(mainFrame);
            aboutBox.setLocationRelativeTo(mainFrame);
        }
        CarcassonneApp.getApplication().show(aboutBox);
    }

    /** This method is called from within the constructor to
     * initialize the form.
     * WARNING: Do NOT modify this code. The content of this method is
     * always regenerated by the Form Editor.
     */
    @SuppressWarnings("unchecked")
    // <editor-fold defaultstate="collapsed" desc="Generated Code">//GEN-BEGIN:initComponents
    private void initComponents() {

        mainBackgroundPanel = new ImagePanel(this.getBackgroundImage());
        gameLaunchMenuPanel = new GridPanel();
        startGameMainMenuButtonPanel = new MainMenuButtonPanel(CarcassonneView.START_GAME_BUTTON_TEXT, this.getClass().getClassLoader().getResource(this.getResourceMap().getString(CarcassonneView.BASE_IMAGE_URL_LOADER, CarcassonneView.MAIN_MENU_BUTTON_IMAGE_NAME)));
        settingsMainMenuButtonPanel = new MainMenuButtonPanel(CarcassonneView.SETTINGS_BUTTON_TEXT, this.getClass().getClassLoader().getResource(this.getResourceMap().getString(CarcassonneView.BASE_IMAGE_URL_LOADER, CarcassonneView.MAIN_MENU_BUTTON_IMAGE_NAME)));
        quitMainMenuButtonPanel = new MainMenuButtonPanel(CarcassonneView.SETTINGS_BUTTON_TEXT, this.getClass().getClassLoader().getResource(this.getResourceMap().getString(CarcassonneView.BASE_IMAGE_URL_LOADER, CarcassonneView.MAIN_MENU_BUTTON_IMAGE_NAME)));
        menuBar = new javax.swing.JMenuBar();
        javax.swing.JMenu fileMenu = new javax.swing.JMenu();
        newGameMenuItem = new javax.swing.JMenuItem();
        javax.swing.JMenuItem exitMenuItem = new javax.swing.JMenuItem();
        javax.swing.JMenu helpMenu = new javax.swing.JMenu();
        javax.swing.JMenuItem aboutMenuItem = new javax.swing.JMenuItem();
        jMenu1 = new javax.swing.JMenu();
        jMenuItem1 = new javax.swing.JMenuItem();
        statusPanel = new javax.swing.JPanel();
        javax.swing.JSeparator statusPanelSeparator = new javax.swing.JSeparator();
        statusMessageLabel = new javax.swing.JLabel();
        statusAnimationLabel = new javax.swing.JLabel();
        progressBar = new javax.swing.JProgressBar();

        mainBackgroundPanel.setName("mainBackgroundPanel"); // NOI18N

        org.jdesktop.application.ResourceMap resourceMap = org.jdesktop.application.Application.getInstance(carcassonne.gui.CarcassonneApp.class).getContext().getResourceMap(CarcassonneView.class);
        gameLaunchMenuPanel.setBackground(resourceMap.getColor("gameLaunchMenuPanel.background")); // NOI18N
        gameLaunchMenuPanel.setMaximumSize(new java.awt.Dimension(254, 128));
        gameLaunchMenuPanel.setMinimumSize(new java.awt.Dimension(254, 128));
        gameLaunchMenuPanel.setName("gameLaunchMenuPanel"); // NOI18N
        gameLaunchMenuPanel.setPreferredSize(new java.awt.Dimension(254, 128));
        gameLaunchMenuPanel.setLayout(new java.awt.GridLayout(3, 1, 2, 1));

        startGameMainMenuButtonPanel.setBackground(resourceMap.getColor("Start Game MainMenuButtonPanel.background")); // NOI18N
        startGameMainMenuButtonPanel.setMaximumSize(new java.awt.Dimension(250, 40));
        startGameMainMenuButtonPanel.setMinimumSize(new java.awt.Dimension(250, 40));
        startGameMainMenuButtonPanel.setName("Start Game MainMenuButtonPanel"); // NOI18N
        startGameMainMenuButtonPanel.setPreferredSize(new java.awt.Dimension(250, 40));

        javax.swing.GroupLayout startGameMainMenuButtonPanelLayout = new javax.swing.GroupLayout(startGameMainMenuButtonPanel);
        startGameMainMenuButtonPanel.setLayout(startGameMainMenuButtonPanelLayout);
        startGameMainMenuButtonPanelLayout.setHorizontalGroup(
            startGameMainMenuButtonPanelLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGap(0, 254, Short.MAX_VALUE)
        );
        startGameMainMenuButtonPanelLayout.setVerticalGroup(
            startGameMainMenuButtonPanelLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGap(0, 42, Short.MAX_VALUE)
        );

        gameLaunchMenuPanel.add(startGameMainMenuButtonPanel);

        settingsMainMenuButtonPanel.setBackground(resourceMap.getColor("settingsMainMenuButtonPanel.background")); // NOI18N
        settingsMainMenuButtonPanel.setMaximumSize(new java.awt.Dimension(250, 40));
        settingsMainMenuButtonPanel.setMinimumSize(new java.awt.Dimension(250, 40));
        settingsMainMenuButtonPanel.setName("settingsMainMenuButtonPanel"); // NOI18N
        settingsMainMenuButtonPanel.setPreferredSize(new java.awt.Dimension(250, 40));

        javax.swing.GroupLayout settingsMainMenuButtonPanelLayout = new javax.swing.GroupLayout(settingsMainMenuButtonPanel);
        settingsMainMenuButtonPanel.setLayout(settingsMainMenuButtonPanelLayout);
        settingsMainMenuButtonPanelLayout.setHorizontalGroup(
            settingsMainMenuButtonPanelLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGap(0, 254, Short.MAX_VALUE)
        );
        settingsMainMenuButtonPanelLayout.setVerticalGroup(
            settingsMainMenuButtonPanelLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGap(0, 42, Short.MAX_VALUE)
        );

        gameLaunchMenuPanel.add(settingsMainMenuButtonPanel);

        quitMainMenuButtonPanel.setBackground(resourceMap.getColor("quitMainMenuButtonPanel.background")); // NOI18N
        quitMainMenuButtonPanel.setMaximumSize(new java.awt.Dimension(250, 40));
        quitMainMenuButtonPanel.setMinimumSize(new java.awt.Dimension(250, 40));
        quitMainMenuButtonPanel.setName("quitMainMenuButtonPanel"); // NOI18N

        javax.swing.GroupLayout quitMainMenuButtonPanelLayout = new javax.swing.GroupLayout(quitMainMenuButtonPanel);
        quitMainMenuButtonPanel.setLayout(quitMainMenuButtonPanelLayout);
        quitMainMenuButtonPanelLayout.setHorizontalGroup(
            quitMainMenuButtonPanelLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGap(0, 254, Short.MAX_VALUE)
        );
        quitMainMenuButtonPanelLayout.setVerticalGroup(
            quitMainMenuButtonPanelLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGap(0, 42, Short.MAX_VALUE)
        );

        gameLaunchMenuPanel.add(quitMainMenuButtonPanel);

        javax.swing.GroupLayout mainBackgroundPanelLayout = new javax.swing.GroupLayout(mainBackgroundPanel);
        mainBackgroundPanel.setLayout(mainBackgroundPanelLayout);
        mainBackgroundPanelLayout.setHorizontalGroup(
            mainBackgroundPanelLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(mainBackgroundPanelLayout.createSequentialGroup()
                .addGap(102, 102, 102)
                .addComponent(gameLaunchMenuPanel, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                .addGap(114, 114, 114))
        );
        mainBackgroundPanelLayout.setVerticalGroup(
            mainBackgroundPanelLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(mainBackgroundPanelLayout.createSequentialGroup()
                .addGap(56, 56, 56)
                .addComponent(gameLaunchMenuPanel, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                .addGap(171, 171, 171))
        );

        menuBar.setName("menuBar"); // NOI18N

        fileMenu.setText(resourceMap.getString("fileMenu.text")); // NOI18N
        fileMenu.setName("fileMenu"); // NOI18N

        newGameMenuItem.setAccelerator(javax.swing.KeyStroke.getKeyStroke(java.awt.event.KeyEvent.VK_N, java.awt.event.InputEvent.CTRL_MASK));
        newGameMenuItem.setText(resourceMap.getString("newGameMenuItem.text")); // NOI18N
        newGameMenuItem.setName("newGameMenuItem"); // NOI18N
        newGameMenuItem.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                newGameMenuItemActionPerformed(evt);
            }
        });
        fileMenu.add(newGameMenuItem);

        javax.swing.ActionMap actionMap = org.jdesktop.application.Application.getInstance(carcassonne.gui.CarcassonneApp.class).getContext().getActionMap(CarcassonneView.class, this);
        exitMenuItem.setAction(actionMap.get("quit")); // NOI18N
        exitMenuItem.setName("exitMenuItem"); // NOI18N
        fileMenu.add(exitMenuItem);

        menuBar.add(fileMenu);

        helpMenu.setText(resourceMap.getString("helpMenu.text")); // NOI18N
        helpMenu.setName("helpMenu"); // NOI18N

        aboutMenuItem.setAction(actionMap.get("showAboutBox")); // NOI18N
        aboutMenuItem.setName("aboutMenuItem"); // NOI18N
        helpMenu.add(aboutMenuItem);

        menuBar.add(helpMenu);

        jMenu1.setText(resourceMap.getString("jMenu1.text")); // NOI18N
        jMenu1.setName("jMenu1"); // NOI18N

        jMenuItem1.setText(resourceMap.getString("jMenuItem1.text")); // NOI18N
        jMenuItem1.setName("jMenuItem1"); // NOI18N
        jMenuItem1.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                jMenuItem1ActionPerformed(evt);
            }
        });
        jMenu1.add(jMenuItem1);

        menuBar.add(jMenu1);

        statusPanel.setName("statusPanel"); // NOI18N

        statusPanelSeparator.setName("statusPanelSeparator"); // NOI18N

        statusMessageLabel.setName("statusMessageLabel"); // NOI18N

        statusAnimationLabel.setHorizontalAlignment(javax.swing.SwingConstants.LEFT);
        statusAnimationLabel.setName("statusAnimationLabel"); // NOI18N

        progressBar.setName("progressBar"); // NOI18N

        javax.swing.GroupLayout statusPanelLayout = new javax.swing.GroupLayout(statusPanel);
        statusPanel.setLayout(statusPanelLayout);
        statusPanelLayout.setHorizontalGroup(
            statusPanelLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addComponent(statusPanelSeparator, javax.swing.GroupLayout.DEFAULT_SIZE, 716, Short.MAX_VALUE)
            .addGroup(statusPanelLayout.createSequentialGroup()
                .addContainerGap()
                .addComponent(statusMessageLabel)
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED, 546, Short.MAX_VALUE)
                .addComponent(progressBar, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addComponent(statusAnimationLabel)
                .addContainerGap())
        );
        statusPanelLayout.setVerticalGroup(
            statusPanelLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(statusPanelLayout.createSequentialGroup()
                .addComponent(statusPanelSeparator, javax.swing.GroupLayout.PREFERRED_SIZE, 2, javax.swing.GroupLayout.PREFERRED_SIZE)
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
                .addGroup(statusPanelLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(statusMessageLabel)
                    .addComponent(statusAnimationLabel)
                    .addComponent(progressBar, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE))
                .addGap(3, 3, 3))
        );

        setComponent(mainBackgroundPanel);
        setMenuBar(menuBar);
        setStatusBar(statusPanel);
    }// </editor-fold>//GEN-END:initComponents

private void newGameMenuItemActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_newGameMenuItemActionPerformed
// TODO add your handling code here:
    Game game = new Game(CarcassonneApp.getGameTileTypeList(), CarcassonneApp.getGameTileList());
    game.startGame();
}//GEN-LAST:event_newGameMenuItemActionPerformed

private void jMenuItem1ActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_jMenuItem1ActionPerformed
// TODO add your handling code here:
    System.out.println("List contains 5? " + NextAvailablePositions.containsPosition(5));
    NextAvailablePositions.removePosition(5);
    System.out.println(NextAvailablePositions.print());
    System.out.println("List contains 5? " + NextAvailablePositions.containsPosition(5));
    
}//GEN-LAST:event_jMenuItem1ActionPerformed

    // Variables declaration - do not modify//GEN-BEGIN:variables
    private javax.swing.JPanel gameLaunchMenuPanel;
    private javax.swing.JMenu jMenu1;
    private javax.swing.JMenuItem jMenuItem1;
    private javax.swing.JPanel mainBackgroundPanel;
    private javax.swing.JMenuBar menuBar;
    private javax.swing.JMenuItem newGameMenuItem;
    private javax.swing.JProgressBar progressBar;
    private javax.swing.JPanel quitMainMenuButtonPanel;
    private javax.swing.JPanel settingsMainMenuButtonPanel;
    private javax.swing.JPanel startGameMainMenuButtonPanel;
    private javax.swing.JLabel statusAnimationLabel;
    private javax.swing.JLabel statusMessageLabel;
    private javax.swing.JPanel statusPanel;
    // End of variables declaration//GEN-END:variables

    private final Timer messageTimer;
    private final Timer busyIconTimer;
    private final Icon idleIcon;
    private final Icon[] busyIcons = new Icon[15];
    private int busyIconIndex = 0;

    private JDialog aboutBox;

	private Image getBackgroundImage() {
		return backgroundImage;
	}

	private void setBackgroundImage(Image backgroundImage) {
		this.backgroundImage = backgroundImage;
	}
}
